package org.helgoboss.text_editor_commandlet

import org.helgoboss.commandlet._
import java.io.File
import org.helgoboss.text_editor.TextEditor
import org.helgoboss.launchlet.{LaunchletExecutionContext, Launchlet}


class TextEditorCommandlet(textEditor: TextEditor) extends Commandlet with Launchlet {
	val id = "org.helgoboss.text-editor"
    
    val name = "Text Editor"
    
	def execute(context: ExecutionContext) = {
        openTextEditor(context.arguments, Some(context.workingDirectory))
	}

    private def openTextEditor(arguments: Seq[String], workingDirectory: Option[File]) = {
        arguments.lift(0) match {
            case Some(path) =>
                val file = new File(path)
                val absoluteFile = if (file.isAbsolute) file else new File(workingDirectory.get, path)
                textEditor.open(files = List(absoluteFile))

            case None =>
                textEditor.open()
        }
    }
    
	def execute(context: LaunchletExecutionContext) = {
        openTextEditor(context.arguments, context.workingDirectory)
	}
}